class Solution {
public:
    vector<int> canSeePersonsCount(vector<int>& heights) {
        int n = heights.size();
        vector<int> answer(n, 0);
        stack<int> stk;

        for (int i = n - 1; i >= 0; --i) {
            int count = 0;
            while (!stk.empty() && heights[i] > stk.top()) {
                count++;
                stk.pop();
            }
            if (!stk.empty()) {
                count++;
            }
            answer[i] = count;
            stk.push(heights[i]);
        }

        return answer;
    }
};